package com.trillion.toyou;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ResultFragment extends Fragment implements OnClickListener {
	View view = null;
	
	// 플래그먼트 관련
	Fragment frag = null;
	FragmentTransaction trans = null;
	
	
	TextView nname = null;
	TextView grade = null;
	TextView department = null;
	TextView company = null;
	TextView phoneNum = null;
	TextView telNum = null;
	TextView email = null;
	TextView address = null;
	
	Button category = null;
	
	
	
	LinearLayout topTitle = null;
	
	Data catchNameCard = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.view = inflater.inflate(R.layout.result_layout, container, false);
		
		MainActivity.page_check = 0;
		
		this.catchNameCard = MainActivity.catch_namecard;
		this.catchNameCard.setCategory("other");
		MainActivity.catch_namecard = null;

		
		this.trans = getFragmentManager().beginTransaction();

		
		
		this.nname = (TextView)this.view.findViewById(R.id.resultLayout_tvNname);
		this.grade = (TextView)this.view.findViewById(R.id.resultLayout_tvGrade);
		this.department = (TextView)this.view.findViewById(R.id.resultLayout_tvDepartment);
		this.company = (TextView)this.view.findViewById(R.id.resultLayout_tvCompany);
		this.phoneNum = (TextView)this.view.findViewById(R.id.resultLayout_tvPhoneNum);
		this.telNum = (TextView)this.view.findViewById(R.id.resultLayout_tvTelNum);
		this.email = (TextView)this.view.findViewById(R.id.resultLayout_tvEmail);
		this.address = (TextView)this.view.findViewById(R.id.resultLayout_tvAddress);

		
		
		this.topTitle = (LinearLayout)this.view.findViewById(R.id.result_layout_top);
		
		this.category = (Button)this.view.findViewById(R.id.resultLayout_handle);
		this.category.setTextColor(Color.WHITE);
		this.category.setText("그룹 : 기타");

		
		
		ImageButton btnInput = (ImageButton)this.view.findViewById(R.id.resultLayout_btnInput);
		ImageButton btnCancle = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCancle);
		ImageButton btnBack = (ImageButton)this.view.findViewById(R.id.resultLayout_btnBack);
		
		
		ImageButton btnCategory1 = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCategory1);
		ImageButton btnCategory2 = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCategory2);
		ImageButton btnCategory3 = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCategory3);
		ImageButton btnCategory4 = (ImageButton)this.view.findViewById(R.id.resultLayout_btnCategory4);

		
		
		this.nname.setText(this.catchNameCard.getNname());
		this.grade.setText(this.catchNameCard.getGrade());
		this.department.setText(this.catchNameCard.getDepartment());
		this.company.setText(this.catchNameCard.getCompany());
		this.phoneNum.setText(this.catchNameCard.getPhoneNum());
		this.telNum.setText(this.catchNameCard.getTelNum());
		this.email.setText(this.catchNameCard.getEmail());
		this.address.setText(this.catchNameCard.getAddress());

		
		
		
		btnInput.setOnClickListener(this);
		btnCancle.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		
		btnCategory1.setOnClickListener(this);
		btnCategory2.setOnClickListener(this);
		btnCategory3.setOnClickListener(this);
		btnCategory4.setOnClickListener(this);
		
		
		return view;
	}

	
	@Override
	public void onResume() {
		new NfcOnOffChange(this.topTitle, R.drawable.top_bar_on_nfc_get, R.drawable.top_bar_off_nfc_get);
		super.onResume();
	}
	
	
	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		
		switch (id) {
		case R.id.resultLayout_btnInput:
			Log.d("받은명함 확인", "id : "  + this.catchNameCard.getNid());
			Log.d("받은명함 확인", "이름 : "  + this.catchNameCard.getNname());
			Log.d("받은명함 확인", "직급 : "  + this.catchNameCard.getGrade());
			Log.d("받은명함 확인", "부서 : "  + this.catchNameCard.getDepartment());
			Log.d("받은명함 확인", "회사 : "  + this.catchNameCard.getCompany());
			Log.d("받은명함 확인", "연락처1 : "  + this.catchNameCard.getPhoneNum());
			Log.d("받은명함 확인", "연락처2 : "  + this.catchNameCard.getTelNum());
			Log.d("받은명함 확인", "이메일 : "  + this.catchNameCard.getEmail());
			Log.d("받은명함 확인", "주소 : "  + this.catchNameCard.getAddress());
			Log.d("받은명함 확인", "입력날짜 : "  + this.catchNameCard.getrDate());
			Log.d("받은명함 확인", "명함 폼 : "  + this.catchNameCard.getStyleId());
			Log.d("받은명함 확인", "카테고리 : "  + this.catchNameCard.getCategory());
			Log.d("받은명함 확인", "대표명함확인 : "  + this.catchNameCard.getMtitle());
			
			
			DBHelper db = new DBHelper(getActivity(), "toyou", null, 1);		
			
			boolean b = db.insertOther(this.catchNameCard);
			
			if(b){
				this.frag = new ListFragment();
				
				this.trans.replace(R.id.root_layout, this.frag);
				this.trans.addToBackStack(null);
				this.trans.commitAllowingStateLoss();
				Toast.makeText(getActivity(), "명함이 정상적으로 저장되었습니다.", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(getActivity(), "명함을 저장하는 도중 오류가 발생되었습니다.", Toast.LENGTH_SHORT).show();
			}
					
			
			break;
		
		case R.id.resultLayout_btnBack:
		case R.id.resultLayout_btnCancle:
			this.frag = new ListFragment();
			
			this.trans.replace(R.id.root_layout, this.frag);
			this.trans.addToBackStack(null);
			this.trans.commitAllowingStateLoss();
			
			Toast.makeText(getActivity(), "저장이 취소되었습니다.", Toast.LENGTH_SHORT).show();
			
			
			
			break;
		
		
			
			
		case R.id.resultLayout_btnCategory1:
			this.category.setText("그룹 : VIP");
			this.catchNameCard.setCategory("vip");
			
			break;
			
			
			
		case R.id.resultLayout_btnCategory2:
			this.category.setText("그룹 : 회사");
			this.catchNameCard.setCategory("company");
			
			break;
			
			
			
		case R.id.resultLayout_btnCategory3:
			this.category.setText("그룹 : 지인");
			this.catchNameCard.setCategory("friends");
			
			break;
			
			
			
		case R.id.resultLayout_btnCategory4:
			this.category.setText("그룹 : 기타");
			this.catchNameCard.setCategory("other");
			
			break;
		}
		
	}
	
}



